using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Configuration;
using System.Drawing;
using System.Data;
using System.Text;
using System.Windows.Forms;

namespace MimeCloud.WindowsForms.Presentation.Controls
{
    public partial class HtmlEditor : UserControl
    {
        private string _text = "";
        private bool _editorReady = false;

        public string Text
        {
            get
            {
                return GetText();
            }
            set
            {
                _text = value;
                SetText();
            }
        }



        public HtmlEditor()
        {
            InitializeComponent();webBrowser1.DocumentCompleted += new WebBrowserDocumentCompletedEventHandler(webBrowser1_DocumentCompleted);
            LoadContentBrowserPage();
        }

        private void LoadContentBrowserPage()
        {
            webBrowser1.Navigate(ConfigurationManager.AppSettings["MimeCloud.WindowsForms.Presentation.Controls.HtmlEditor.EditorUrl"]);
        }

        private void SetText()
        {
            if (_editorReady && !string.IsNullOrEmpty(_text))
            {
                Object[] objArray = new Object[1];
                objArray[0] = (Object) _text;
                webBrowser1.Document.InvokeScript("setValue", objArray);
            }
        }

        private string GetText()
        {
            if (!_editorReady)
                return _text;
            else
                return
                    (string) webBrowser1.Document.InvokeScript("getValue");
        }

        private void HtmlEditor_Load(object sender, EventArgs e)
        {
            
        }

        void webBrowser1_DocumentCompleted(object sender, WebBrowserDocumentCompletedEventArgs e)
        {
            //set text
            _editorReady = true;
            SetText();
        }

    }
}